﻿using System.Collections.Generic;
using Minecraft.CacheRedis;
using Minecraft.Config;
using Minecraft.Model;
using Minecraft.Model.mysql;

namespace Minecraft.BLL.mysql
{
    /// <summary>
    /// 成就Bll
    /// </summary>
    public class AchievemetnsBLL : BaseBLL
    {

        private static RedisCacheHelper redisCacheHelper = new RedisCacheHelper();
        private static RedisKey redisKeyEnum = RedisKey.Achievement;


        /// <summary>
        /// 
        /// </summary>
        /// <param name="playerId"></param>
        /// <param name="fromCache"></param>
        /// <returns></returns>
        public static List<AchievementModel> GetAchievements(int playerId, out bool fromCache)
        {
            fromCache = false;
            string redisKey = RedisKeyHelper.GetRedisKeyName(redisKeyEnum, playerId.ToString());
            var cacheList = redisCacheHelper.StringGet<List<AchievementModel>>(redisKey);
            if (cacheList != null)
            {
                fromCache = true;
                return cacheList;
            }
            else
            {
                var model = new AchievementModel();
                var list = GetList(model, new KeyValuePair<string, int>(nameof(model.PlayerId), playerId));
                redisCacheHelper.StringSet(redisKey, list, CommonConfig.DefRedisExpiry);
                return list;
            }
        }



        /// <summary>
		/// 批量更新玩家的成就信息（对于数据库操作时，先删除后插入）
		/// </summary>
		/// <param name="isCacheAfterDbOpt">缓存后是否进行数据库操作</param>
		public static bool BatchUpdate(List<AchievementModel> list, int playerId, bool isCacheAfterDbOpt)
        {
            string redisKey = RedisKeyHelper.GetRedisKeyName(redisKeyEnum, playerId.ToString());
            redisCacheHelper.StringSet(redisKey, list, CommonConfig.DefRedisExpiry);
            if (isCacheAfterDbOpt)
            {
                var model = new AchievementModel();
                return BatchUpdate(list, new KeyValuePair<string, int>(nameof(model.PlayerId), playerId));
            }
            return true;
        }
    }
}
